//
//  WXPOrderDeferDetailVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/18.
//

#import "WXPOrderDeferDetailVC.h"
#import "WXPOrderDeferDetailHeaderView.h"
#import "WXPOrderDeferDetailFooterView.h"
#import "WXPDeferRepaymentVC.h"

@interface WXPOrderDeferDetailVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;
@property (nonatomic, strong) WXPOrderDeferDetailHeaderView * headerView;
@property (nonatomic, strong) WXPOrderDeferDetailFooterView * footerView;
@property (nonatomic, strong) WXPTwoBtnPopUpWindowView * notaWindowView;
@property (nonatomic, strong) ModelPostpone * modelPostpone;

@end

@implementation WXPOrderDeferDetailVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        [_navView resetViewWithTitle:@"Detalles del préstamo"];
        _navView.leftTop = XY(0, 0);
    }
    return  _navView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}
- (WXPOrderDeferDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [WXPOrderDeferDetailHeaderView new];
        WEAKSELF
        _headerView.blockDeferRepayment = ^{
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.notaWindowView];
        };
    }
    return _headerView;
}
- (WXPOrderDeferDetailFooterView *)footerView{
    if (!_footerView) {
        _footerView = [WXPOrderDeferDetailFooterView new];
        WEAKSELF
        _footerView.blockOpenBtnClick = ^{
            weakSelf.tableView.tableFooterView = weakSelf.footerView;
        };
    }
    return _footerView;
}
// 弹窗
- (WXPTwoBtnPopUpWindowView *)notaWindowView{
    if (!_notaWindowView) {
        _notaWindowView = [WXPTwoBtnPopUpWindowView new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Nota";
        model.content = @"Por favor, haga el pago hoy mismo para solicitar la prórroga de manera exitosa";
        model.rightName = @"confirmar";
        model.isHide = true;
        [_notaWindowView resetWithModel:model];
        [_notaWindowView resetOnlyShowRightBtn];
        WEAKSELF
        _notaWindowView.blockRightClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_DelayRepaymentConfirm action:KEY_Save extInfo:nil];
            [weakSelf.notaWindowView removeFromSuperview];
            WXPDeferRepaymentVC * vc = [WXPDeferRepaymentVC new];
            vc.orderDetailModel = weakSelf.orderDetailModel;
            vc.modelPostpone = weakSelf.modelPostpone;
            [Global_Nav pushViewController:vc animated:true];
        };
    }
    return _notaWindowView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_DelayRepaymentConfirm action:KEY_Enter extInfo:@{@"loanOrderId": self.orderDetailModel.orderNo}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self requestDetail];
}
- (void)configUI{
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.agreementView];
    self.tableView.height = self.agreementView.top-self.navView.bottom-10;
    self.tableView.top = self.navView.bottom+10;
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
}

#pragma mark - request
- (void)requestDetail{
    [RequestApi requestCalculateDelayWithOrderNo:self.orderDetailModel.orderNo delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        self.modelPostpone = [GlobalMethod exchangeDicToModel:response modelName:@"ModelPostpone"];
        [self.headerView resetViewOrderModel:self.orderDetailModel deferModel:self.modelPostpone];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Prórroga de plazo";
        model.subTitle = @"Gasto de prórrog($)";
        model.content = @"Nueva fecha de vencimiento";
        [self.headerView.topMoneyView resetStateLabel:model];
        self.tableView.tableHeaderView = self.headerView;
        
        NSMutableArray * pairsArray = [NSMutableArray array];
        [pairsArray addObjectsFromArray:self.modelPostpone.extensionDetails];
        [self.footerView resetView:pairsArray isOpen:false];
        self.tableView.tableFooterView = self.footerView;
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
